<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class Post extends MX_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('Admin_Layout');
        $this->load->model('post/post_model');
        $this->load->library('pagination');
        $this->load->helper('url');
        $this->load->library('table');
    }
    
    public function index()
    {
        $this->mycommon->authAdmin();
        $this->organize();
    }
    
    public function add()
    {
        $this->mycommon->authAdmin();
        $data = array();
        $this->load->model('category/category_model');
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        $this->load->library('Minifier');
        $data['error'] = '';
        if ($this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('upload', 'Ảnh minh họa', 'required');
            $this->form_validation->set_rules('title', 'Tiêu để', 'required|min_lenght[10]');
            $this->form_validation->set_rules('desc', 'Mô tả', 'required|min_lenght[15]');
            $this->form_validation->set_rules('content', 'Nội dung', 'required|min_lenght[30]');
            $this->form_validation->set_message('required', '%s không được để trống');
            $this->form_validation->set_message('min_length', '%s phải có ít nhất %d ký tự');
            if ($this->form_validation->run() == TRUE) {
                $array = array(
                    'title'  =>  $this->input->post('title'),
                    'desc'  =>  $this->input->post('desc'),
                    'content'  =>  $this->input->post('content'),
                    'linkimg'  =>  $this->input->post('upload'),
                    'modified'  =>  time() + 7*3600,
                    'status'    =>  '1',
                );
                $this->load->model('post/post_model');
                if ($this->post_model->addNew($array)) {
                    redirect('admin/post/organize');
                } else {
                    $data['error'] = 'Có lỗi xảy ra, vui lòng thử lại';
                }
            }
        }
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->minifier->AddJsNoPack('uploadify/jquery.uploadify.min');
        $this->minifier->AddJsNoPack('ckeditor/ckeditor');
        $this->minifier->AddJsNoPack('ckeditor/adapters/jquery');
        $this->admin_layout->render('post/add_post_view', $data);
    }
    
    public function organize($page = '')
    {
        $this->mycommon->authAdmin();
        $data = array();
        $page = ($page != '') ? intval($page) : 1;
        $keyword = $this->input->get('k');
        $numPost = $this->post_model->count_all($keyword);
        $config['base_url'] = '/admin/post/index';
        $config['total_rows'] = $numPost;
        $config['per_page'] = 10;
        $config['use_page_numbers'] = TRUE;
        $config['first_link'] = '«';
        $config['last_link'] = '»';
        $config['cur_tag_open'] = '<a href="#" class="active">';
        $config['cur_tag_close'] = '</a>';
        $config['num_links'] = '6';
        $config['cur_page'] = $page;
        $config['uri_segment'] = 3;
        $config["suffix"] = ".html?k=.$keyword";
        $this->pagination->initialize($config);
        $start = ($page - 1) * $config['per_page'];
        $data['pagination'] = $this->pagination->create_links();
        $data['error'] = '';
        $data['headTitle'] = 'Quản lý tin tức';
        $data['current'] = 17;
        $this->load->helper('text');
        $data['keyword'] = $keyword;
        $tmpPost = $this->post_model->getPost($config['per_page'],$start,$keyword);
        $data['allPostInfo'] = $tmpPost;
        foreach($tmpPost as $k=>$v) {
            $data['allPostInfo'][$k]['title'] = word_limiter($v['title'], 8);
            $tmpTime = date('d-m-Y H:i:s', strtotime($v['modified']));
            $data['allPostInfo'][$k]['modified'] = $tmpTime;
        }
        $this->admin_layout->render('post/organize_view', $data);
    }
    
    public function hide($post = '')
    {
        $this->mycommon->authAdmin();
        if ($post == '' || intval($post) <= 0) {
            show_404();
        }
        $post = intval($post);
        $data = array(
            'status'    =>  '0'
        );
        $this->load->model('post/post_model');
        $this->post_model->updatePost($post, $data);
        redirect('admin/post/organize');
    }
    
    public function show($post = '')
    {
        $this->mycommon->authAdmin();
        if ($post == '' || intval($post) <= 0) {
            show_404();
        }
        $post = intval($post);
        $data = array(
            'status'    =>  '1'
        );
        $this->load->model('post/post_model');
        $this->post_model->updatePost($post, $data);
        redirect('admin/post/organize');
    }
    
    public function delete($post = '')
    {
        $this->mycommon->authAdmin();
        if ($post == '' || intval($post) <= 0) {
            show_404();
        }
        $post = intval($post);
        $this->load->model('post/post_model');
        $this->post_model->deletePost($post);
        redirect('admin/post/organize');
    }
    
    public function edit($post = '')
    {
        $this->mycommon->authAdmin();
        $data = array();
        $data['error'] = '';
        if ($post == '' || intval($post) <= 0) {
            show_404();
        }
        $post = intval($post);
        $this->load->model('post/post_model');
        $this->load->model('category/category_model');
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        $tmpInfo = $this->post_model->postInfo($post);
        if(count($tmpInfo) == 0)
            show_404();
        if ($this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('upload', 'Ảnh minh họa', 'required');
            $this->form_validation->set_rules('title', 'Tiêu để', 'required|min_lenght[10]');
            $this->form_validation->set_rules('desc', 'Mô tả', 'required|min_lenght[15]');
            $this->form_validation->set_rules('content', 'Nội dung', 'required|min_lenght[30]');
            $this->form_validation->set_message('required', '%s không được để trống');
            $this->form_validation->set_message('min_length', '%s phải có ít nhất %d ký tự');
            if ($this->form_validation->run() == TRUE) {
                $array = array(
                    'title'  =>  $this->input->post('title'),
                    'desc'  =>  $this->input->post('desc'),
                    'content'  =>  $this->input->post('content'),
                    'linkimg'  =>  $this->input->post('upload'),
                    'modified'  =>  time() + 7*3600,
                    'status'    =>  '1',
                );
                if ($this->post_model->updatePost($post, $array)) {
                    redirect('admin/post/organize');
                } else {
                    $data['error'] = 'Có lỗi xảy ra, vui lòng thử lại';
                }
            }
        }
        $data['postInfo'] = $tmpInfo[0];
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->minifier->AddJsNoPack('uploadify/jquery.uploadify.min');
        $this->minifier->AddJsNoPack('ckeditor/ckeditor');
        $this->minifier->AddJsNoPack('ckeditor/adapters/jquery');
        $this->admin_layout->render('post/edit_post_view', $data);
        
    }
    

}
?>
